<?php
/**
 * 这是一个魔术方法的示例
 * 在这个例子中，使用了数组来保存变量，数组元素可以随意增加，即使不用数组，PHP对象属性也可以直接添加
 * User: ford
 * Date: 15-6-17
 * Time: 下午4:23
 */

class SetGet {
	protected $members = array();
	public $var;

	public function __get($arg) {
		echo "Function __get() is called.<br />";
		if(array_key_exists($arg, $this->members)) {
			return $this->members[$arg];
		} else {
			return "No such luck!<br />\n";
		}
	}

	public function __set($key, $val) {
		echo "Function __set() is called.<br />";
		$this->members[$key] = $val;
	}

	public function __isset($arg) {
		echo "Function __isset() is called.";
		return isset($this->members[$arg]);
	}

}

$x = new SetGet();
echo $x->speedLimit;

$x->speedLimit= "65 MPH";

if(isset($x->speedLimit)) {
//	print_r($x);
	echo "Speed limit is set to $x->speedLimit.<br />";
}

$x->speedLimit = NULL;
if (empty($x->speedLimit)) {
	print "The method __isset() was called.\n";
} else {
	print "The __isset() method wasn't called.\n";
}
$x->var = 'dd';	//设置或访问己存在的变量则不会调这次用到的三个魔术方法
echo $x->var;

